#include "cornerAxis.h"
#include <math.h>

using namespace std;


void Viewer::draw()
{
  float nbSteps = 200.0;

  glBegin(GL_QUAD_STRIP);
  for (float i=0; i<nbSteps; ++i)
  {
    float ratio = i/nbSteps;
    float angle = 21.0*ratio;
    float c = cos(angle);
    float s = sin(angle);
    float r1 = 1.0 - 0.8*ratio;
    float r2 = 0.8 - 0.8*ratio;
    float alt = ratio - 0.5;
    const float nor = 0.5;
    const float up = sqrt(1.0-nor*nor);
    glColor3f(1.0-ratio, 0.2f, ratio);
    glNormal3f(nor*c, up, nor*s);
    glVertex3f(r1*c, alt, r1*s);
    glVertex3f(r2*c, alt+0.05, r2*s);
  }
  glEnd();
}

void Viewer::drawCornerAxis()
{
	int viewport[4];
	int scissor[4];

	// The viewport and the scissor are changed to fit the lower left
	// corner. Original values are saved.
	glGetIntegerv(GL_VIEWPORT, viewport);
	glGetIntegerv(GL_SCISSOR_BOX, scissor);

	// Axis viewport size, in pixels
	const int size = 150;
	glViewport(0,0,size,size);
	glScissor(0,0,size,size);

	// The Z-buffer is cleared to make the axis appear over the
	// original image.
	glClear(GL_DEPTH_BUFFER_BIT);

	// Tune for best line rendering
    glDisable(GL_LIGHTING);
	glLineWidth(3.0);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(-1, 1, -1, 1, -1, 1);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glMultMatrixd(camera()->orientation().inverse().matrix());

	glBegin(GL_LINES);
	glColor3f(1.0, 0.0, 0.0);
	glVertex3f(0.0, 0.0, 0.0);
	glVertex3f(1.0, 0.0, 0.0);

	glColor3f(0.0, 1.0, 0.0);
	glVertex3f(0.0, 0.0, 0.0);
	glVertex3f(0.0, 1.0, 0.0);
	
	glColor3f(0.0, 0.0, 1.0);
	glVertex3f(0.0, 0.0, 0.0);
	glVertex3f(0.0, 0.0, 1.0);
	glEnd();

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();

	glEnable(GL_LIGHTING);

	// The viewport and the scissor are restored.
	glScissor(scissor[0],scissor[1],scissor[2],scissor[3]);
	glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);
}

// The thumbnail has to be drawn at the very end to allow a correct
// display of the visual hints (axes, grid, etc).
void Viewer::postDraw()
{
  postDraw();
  drawCornerAxis();
}

void Viewer::init()
{
  restoreStateFromFile();
  help();
}

QString Viewer::helpString() const
{
  QString text("<h2>C o r n e r A x i s</h2>");
  text += "A world axis representation is drawn in the lower left corner, so that one always sees how the scene is oriented.<br><br>";

  text += "The axis is drawn in <code>postDraw()</code> with appropriate ortho camera parameters. ";
  text += "<code>glViewport</code> and <code>glScissor</code> are used to restrict the drawing area.";
  return text;
}
